
/**
  ******************************************************************************
  * Copyright 2021 The Microbee Authors. All Rights Reserved.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  * 
  * http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  * 
  * @file       rcprotocol_ppmsum.c
  * @author     baiyang
  * @date       2022-7-5
  ******************************************************************************
  */

/*----------------------------------include-----------------------------------*/
#include "rcprotocol_ppmsum.h"
/*-----------------------------------macro------------------------------------*/

/*----------------------------------typedef-----------------------------------*/

/*---------------------------------prototype----------------------------------*/
static void process_pulse(rcprotocol_backend_t backend, uint32_t width_s0, uint32_t width_s1);
/*----------------------------------variable----------------------------------*/
static struct rcprotocol_backend_ops ppmsum_ops = {.rcprotocol_backend_destructor = NULL,
                                                 .process_pulse = process_pulse,
                                                 .process_byte = NULL,
                                                 .process_handshake = NULL,
                                                 .start_bind = NULL,
                                                 .update = NULL};
/*-------------------------------------os-------------------------------------*/

/*----------------------------------function----------------------------------*/
void rcprotocol_ppmsum_ctor(rcprotocol_ppmsum_t ppmsum)
{
    rcprotocol_backend_ctor(&ppmsum->backend, &ppmsum_ops);
}

rcprotocol_backend_t rcprotocol_ppmsum_probe()
{
    rcprotocol_ppmsum_t ppmsum = (rcprotocol_ppmsum_t)rt_malloc(sizeof(struct rcprotocol_ppmsum));

    if (ppmsum) {
        rt_memset(ppmsum, 0, sizeof(struct rcprotocol_ppmsum));
        rcprotocol_ppmsum_ctor(ppmsum);

        return (rcprotocol_backend_t)ppmsum;
    }

    return NULL;
}

/*
  process a SBUS input pulse of the given width
 */
static void process_pulse(rcprotocol_backend_t backend, uint32_t width_s0, uint32_t width_s1)
{
    rcprotocol_ppmsum_t ppmsum = (rcprotocol_ppmsum_t)backend;

    if (width_s0 == 0 || width_s1 == 0) {
        //invalid data: reset frame
        ppmsum->ppm_state._channel_counter = -1;
        return;
    }
    uint32_t width_usec = width_s0 + width_s1;
    if (width_usec >= 2700) {
        // a long pulse indicates the end of a frame. Reset the
        // channel counter so next pulse is channel 0
        if (ppmsum->ppm_state._channel_counter >= MIN_RCIN_CHANNELS) {
            rcprotocol_backend_add_input(backend, ppmsum->ppm_state._channel_counter, ppmsum->ppm_state._pulse_capt, false, -1, -1);
        }
        ppmsum->ppm_state._channel_counter = 0;
        return;
    }
    if (ppmsum->ppm_state._channel_counter == -1) {
        // we are not synchronised
        return;
    }

    /*
      we limit inputs to between 700usec and 2300usec. This allows us
      to decode SBUS on the same pin, as SBUS will have a maximum
      pulse width of 100usec
     */
    if (width_usec > 700 && width_usec < 2300) {
        // take a reading for the current channel
        // buffer these
        ppmsum->ppm_state._pulse_capt[ppmsum->ppm_state._channel_counter] = width_usec;

        // move to next channel
        ppmsum->ppm_state._channel_counter++;
    }

    // if we have reached the maximum supported channels then
    // mark as unsynchronised, so we wait for a wide pulse
    if (ppmsum->ppm_state._channel_counter >= MAX_RCIN_CHANNELS) {
        rcprotocol_backend_add_input(backend, ppmsum->ppm_state._channel_counter, ppmsum->ppm_state._pulse_capt, false, -1, -1);
        ppmsum->ppm_state._channel_counter = -1;
    }

}

/*------------------------------------test------------------------------------*/


